package com.book.service.impl;

import com.book.api.ReaderApi;
import com.book.pojo.MonthlyPass;
import com.book.pojo.MonthlyRanking;
import com.book.mapper.MonthlyRankingDao;
import com.book.service.MonthlyRankingService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author cxy
 * @since 2023-10-14 09:16:33
 */
@Service
public class MonthlyRankingServiceImpl extends ServiceImpl<MonthlyRankingDao, MonthlyRanking> implements MonthlyRankingService {
    @Autowired
    private MonthlyRankingDao monthlyRankingDao;
    @Autowired
    private ReaderApi readerApi;

    @Override
    public List<MonthlyRanking> monthlyRanking() {
        List<MonthlyPass> monthlyPasses = readerApi.monthlyRanking();
        MonthlyRanking ranking = new MonthlyRanking();
        for (MonthlyPass monthlyPass : monthlyPasses) {
            monthlyRankingDao.deleteByBookId(monthlyPass.getBookId());
            ranking.setBookId(monthlyPass.getBookId());
            ranking.setMonthlySum(monthlyPass.getMonthlySum());
            monthlyRankingDao.insert(ranking);
        }
        return monthlyRankingDao.selectList(null);
    }

    @Override
    public void deleteByBookId(Long bookid) {
        monthlyRankingDao.deleteByBookId(bookid);
    }

    @Override
    public void addMonthlyRanking(MonthlyRanking monthlyRanking) {
        monthlyRankingDao.insert(monthlyRanking);
    }
}
